package com.bootdo.app;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.*;
import com.bootdo.system.service.GuessItemService;
import com.bootdo.system.service.GuessLogService;
import com.bootdo.system.service.GuessService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app/guess/")
public class AppGuessController extends BaseController {

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private GuessService guessService;

    @Autowired
    private GuessItemService guessItemService;

    @Autowired
    private GuessLogService guessLogService;

    /**
     * 获取竞猜参与记录
     * @param params
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/guessLogList")
    public R guessLogList(@RequestParam Map<String, Object> params, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        //用户信息
        UserDO userDO = (UserDO) r.get("data");
        //获取用户角色
        List<Long> list = userRoleDao.listRoleId(userDO.getUserId());
        if(!CollectionUtils.isEmpty(list)) {
            if(!list.contains(1l)) {
                //普通用户
                params.put("createBy",userDO.getUserId());
            }
        }
        //查询列表数据
        Query query = new Query(params);
        List<GuessLogDO> guessList = guessLogService.list(query);
        int total = guessLogService.count(query);
        PageUtils pageUtils = new PageUtils(guessList, total);
        return R.ok(pageUtils);
    }

    /**
     * 获取开奖信息
     */
    @ResponseBody
    @PostMapping("/getGuessDetail")
    public R save(GuessDO guessDO, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        Map<String,Object> map = new HashMap<String, Object>();
        List<GuessDO> list = guessService.list(map);
        guessDO = list.get(0);
        //guessDO = guessService.get(guessDO.getId());
        map.put("guessId",guessDO.getId());
        List<GuessItemDO> list1 = guessItemService.list(map);
        //计算竞猜赔率
        double guessOdds = 1.95;
        GuessItemDO guessItemDO1 = list1.get(0);
        GuessItemDO guessItemDO2 = list1.get(1);
        try {
            double guessOdds1 = new BigDecimal(Double.valueOf(guessItemDO1.getGuessIntegral())/Double.valueOf(guessItemDO2.getGuessIntegral())).doubleValue();
            double guessOdds2 = new BigDecimal(Double.valueOf(guessItemDO2.getGuessIntegral())/Double.valueOf(guessItemDO1.getGuessIntegral())).doubleValue();
            guessItemDO1.setGuessOdds(guessOdds1);
            guessItemDO2.setGuessOdds(guessOdds2);
            if(guessOdds == 1) {
                guessOdds = 1.95;
                guessItemDO1.setGuessOdds(guessOdds);
                guessItemDO2.setGuessOdds(guessOdds);
            }
        }catch (Exception e) {
            guessOdds = 1.95;
            guessItemDO1.setGuessOdds(guessOdds);
            guessItemDO2.setGuessOdds(guessOdds);
        }
        List<GuessItemDO> list2 = new ArrayList<GuessItemDO>();
        list2.add(guessItemDO1);
        list2.add(guessItemDO2);
        guessDO.setGuessItemDOList(list2);
        map.put("guessDO",guessDO);
        return R.ok(map);
    }

    /**
     * 保存竞猜信息
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(GuessLogDO guessLogDO, GuessItemDO guessItemDO, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        //用户信息
        UserDO userDO = (UserDO) r.get("data");
        GuessDO guessDO = guessService.get(guessLogDO.getGuessId());
        if(null != guessDO && !StringUtils.equals("2",guessDO.getGuessState())) {
            return R.error("当前竞猜活动没有进行，无法下注");
        }
        guessLogDO.setCreateBy(userDO.getUserId());
        r = guessLogService.save(guessLogDO,guessItemDO);
        return r;
    }

    /**
     * 竞猜开奖
     */
    @ResponseBody
    @PostMapping("/drawPrize")
    public R drawPrize(GuessLogDO guessLogDO, GuessItemDO guessItemDO, HttpServletRequest request){
        R r = checkToken(request);
        if(!StringUtils.equals(String.valueOf(r.get("code")),"0")) {
            return r;
        }
        //用户信息
        UserDO userDO = (UserDO) r.get("data");
        guessLogDO.setCreateBy(userDO.getUserId());
        r = guessLogService.drawPrize(guessLogDO,guessItemDO);
        return r;
    }

}
